﻿package com.leqicheng.tenon.wxapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.leqicheng.tenon.login.weixin.WeixinConstants;
import com.leqicheng.tenon.model.EntryInfoModel;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelbase.BaseResp.ErrCode;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler,
        OnClickListener {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    private Context mContext;

    private TextView mNavigationTitle;

    private ImageButton mNavigationBack;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = this;

        this.api = WXAPIFactory
                .createWXAPI(this, WeixinConstants.APP_ID, false);
        this.api.handleIntent(this.getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        this.setIntent(intent);
        this.api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        Intent intent = new Intent();
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
            case ErrCode.ERR_OK:
                Toast.makeText(mContext, "支付完成 ", Toast.LENGTH_SHORT).show();
                EntryInfoModel model = EntryInfoModel.getInstance();
                // model.setThirdPartToken(token);
                intent.putExtra("pay_status", true);
                intent.setAction("weixin pay complete");
                mContext.sendBroadcast(intent);
                finish();
                break;
            case ErrCode.ERR_COMM:
                Toast.makeText(mContext, "支付遇到错误 ", Toast.LENGTH_SHORT).show();
                intent.putExtra("pay_status", false);
                intent.setAction("weixin pay complete");
                mContext.sendBroadcast(intent);
                finish();
                break;
            case ErrCode.ERR_USER_CANCEL:
                Toast.makeText(mContext, "支付取消 ", Toast.LENGTH_SHORT).show();
                intent.putExtra("pay_status", false);
                intent.setAction("weixin pay complete");
                mContext.sendBroadcast(intent);
                finish();
                break;

            default:
                break;
            }
        }

    }
    

    @Override
    public void onClick(View v) {
    }
}